function [levels] = sc_levels();

h = findobj('Tag', 'SCH'); % check for pre-existing window
if(isempty(h)) % if none, make one
   h = figure('Tag', 'SCH', 'Name', 'Single Channel Histogram', 'NumberTitle', 'off');
end
figure(h); % otherwise, select it
clf; % always clear the window...
set(gcf, 'Backingstore', 'off');

DFILE=datac('getdfile');
c=datac('geti'); % get the currents;
v=datac('getv');
ord = v_order(v, DFILE, 110, 300);
vt=-10;
rx=find(abs(ord.v-vt) < 1);
rl=ord.ro([rx],:);
if(isempty(rl))
	disp(sprintf('No records for V=%f', vt))
	return;
end;
cx=c(:,[52:1500]); % window the data;
vx=v(:,[52:1500]);
c1=cx([rl],:); % just get one trace to test.
[nr,ds] = size(c1);
v1=vx([91, 93, 95],:);
mv=mean(mean(v1));
% filter the current traces
% According to Colquhoun and Sigworth (Pratical Analysis of Records, Chapter 19 in
% Sakmann and Neher, Single Channel Recording)
% the optimal filter is Gaussian for the first
% pass of analysis. So, we use here their Gaussian filter.
% the filter itself is in Gaussian.m in pbm_tools

	fsamp = 1000/(DFILE.nr_channel*DFILE.rate); % get sampling frequency
	fco = 0.05;		% cutoff frequency units of sample frequency
	if(fco < 1) % if fco is > 1 then this is not a filter!
	   c1 = gaussian(c1, fco); % filter all the traces...
	end
% now compute amplitude histogram for EACH trace... and fit with 2 gaussians
pa=[];
for i=1:nr
	x1=min(min(c1));
	x2=max(max(c1));
	nbins=50; % determine number of bins we can use...
	binw=(x2-x1)/nbins;
	bin=[x1:binw:x2]; % make the bin array
	
	[h1, ix] = hist(c1(i,:), bin);
	sh= h1; % =sum(h1')

	a0=0; a1=max(sh)/2; [mx, imx] = max(sh); m1=bin(imx); s1=1;
	a2=max(sh)/2; m2=m1+5; s2=1;
	[fpar, chisq, niter, gfit] = mrqfit('gaussian', [a0 a1 m1 s1], bin, sh, [], [0 1 1 1 ], [0 0 -50 0], [0 1e6 50 500], 1000, []);
	[f2par, chisq2, niter, g2fit] = mrqfit('gaussian', [fpar(1) fpar(2) fpar(3) fpar(4) a2 m2 s2], bin, sh, [], [0 1 1 1 1 1 1], [], [], 1000, []);%f2par
	hold on
	plot(bin, sh);
	plot(bin, gfit, 'Color', 'r');
	plot(bin, g2fit, 'Color', 'g');
	hold off
	p=abs(f2par(3)-f2par(6));
	pa(i,:)=[p chisq chisq2];
end;

[a u]=max([f2par(2) f2par(5)]); % find largest peak
if(u==1)
	sigma=f2par(4);
else
	sigma=f2par(7);
end;
sigma
levels=pa;
minchi=min(min([pa(:,2) pa(:,3)]))
u=find((pa(:,1)>sigma & pa(:,1)<50) & (pa(:,3) < 0.5*pa(:,2)) & (pa(:,3) < 10*minchi));
pa([u],1)
pam=mean(pa([u],1))
%scg=id/mv;
disp(sprintf('mean Chan amp: %7.1f pA at %7.2f mV', pam, mv))
%set(gca, 'Yscale', 'log');


h2 = findobj('Tag', 'data'); % check for pre-existing window
if(isempty(h2)) % if none, make one
   h2 = figure('Tag', 'data', 'Name', 'Single Channel data', 'NumberTitle', 'off');
end
figure(h2); % otherwise, select it
clf; % always clear the window...
set(gcf, 'Backingstore', 'off');

plot(c1');
